<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片上传测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        .upload-container {
            border: 2px dashed #ccc;
            padding: 20px;
            text-align: center;
            margin-bottom: 20px;
        }
        #preview {
            max-width: 100%;
            margin-top: 20px;
            display: none;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        #status {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
    </style>
</head>
<body>
    <h1>图片上传测试</h1>
    
    <div class="upload-container">
        <input type="file" id="imageInput" accept="image/*">
        <p>或将图片拖放到此处</p>
        <img id="preview" alt="图片预览">
    </div>
    
    <button id="uploadBtn">上传图片</button>
    
    <div id="status"></div>
    
    <script>
        const imageInput = document.getElementById('imageInput');
        const preview = document.getElementById('preview');
        const uploadBtn = document.getElementById('uploadBtn');
        const statusDiv = document.getElementById('status');
        
        // 图片预览功能
        imageInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(event) {
                    preview.src = event.target.result;
                    preview.style.display = 'block';
                };
                reader.readAsDataURL(file);
            }
        });
        
        // 拖放功能
        const uploadContainer = document.querySelector('.upload-container');
        
        uploadContainer.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadContainer.style.borderColor = '#4CAF50';
        });
        
        uploadContainer.addEventListener('dragleave', () => {
            uploadContainer.style.borderColor = '#ccc';
        });
        
        uploadContainer.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadContainer.style.borderColor = '#ccc';
            
            const file = e.dataTransfer.files[0];
            if (file && file.type.startsWith('image/')) {
                imageInput.files = e.dataTransfer.files;
                
                const reader = new FileReader();
                reader.onload = function(event) {
                    preview.src = event.target.result;
                    preview.style.display = 'block';
                };
                reader.readAsDataURL(file);
            }
        });
        
        // 上传功能
        uploadBtn.addEventListener('click', async () => {
            const file = imageInput.files[0];
            if (!file) {
                showStatus('请先选择图片文件', 'error');
                return;
            }
            
            try {
                const formData = new FormData();
                formData.append('image', file, encodeURIComponent(file.name));
                
                const response = await fetch('/files/upload-image', {
                    method: 'POST',
                    headers: {
                        'accept': '*/*'
                    },
                    body: formData
                });
                
                if (response.ok) {
                    const result = await response.json();
                    showStatus(`上传成功: ${JSON.stringify(result)}`, 'success');
                } else {
                    const error = await response.text();
                    showStatus(`上传失败: ${error}`, 'error');
                }
            } catch (error) {
                showStatus(`请求出错: ${error.message}`, 'error');
            }
        });
        
        function showStatus(message, type) {
            statusDiv.textContent = message;
            statusDiv.className = type;
        }
    </script>
</body>
</html>
